import mx.controls.*;
import mx.events.ListEvent;

private function addNewEntry():void {
	var x:XML = <entry name="" text="" suggestion="" translation="" />;
	
	x.@name = "label_" + new Date().time.toString();
	
	for each (var l:XML in project.locale) {
		project.locale.(@code == l.@code)[0].appendChild(x.copy());
	}
	mlDatagrid.invalidateDisplayList();
	mlDatagrid.selectedIndex = mlDatagrid.dataProvider.length - 1;
	mlDatagrid.scrollToIndex(mlDatagrid.dataProvider.length);
	entryText.text = mlDatagrid.selectedItem.@text;
	newEntryName.text = oldEntryName.text = mlDatagrid.selectedItem.@name;
	lastEntryName = newEntryName.text;
	entryText.enabled = newEntryName.enabled = true;
	newEntryName.setFocus();
	projectModified = true;
}

private function deleteEntry():void {
	if (mlDatagrid.selectedIndex >= 0) {
		var nm:String = mlDatagrid.selectedItem.@name;
	 
		for each (var l:XML in project.locale) {
			delete project.locale.(@code == l.@code).entry.(@name == nm)[0];
		} 
		
		entryText.text = oldEntryName.text = newEntryName.text = "";
		projectModified = true;
	}
}

private function onMlItemClick(event:ListEvent):void {
	if (mlDatagrid.selectedIndex >= 0) {
		entryText.text = mlDatagrid.selectedItem.@text;
		newEntryName.text = oldEntryName.text = mlDatagrid.selectedItem.@name;
		lastEntryName = newEntryName.text;
		entryText.enabled = newEntryName.enabled = true;
		entryText.setFocus();
	}
	else {
		entryText.enabled = newEntryName.enabled = false;
		entryText.text = newEntryName.text = oldEntryName.text = "";
	}
}

private function onEntryChange():void {
	if (mlDatagrid.selectedIndex >= 0) {
		var n:Number = mlDatagrid.selectedIndex;
		project.locale.(@code == masterLocale).entry.(@name == mlDatagrid.selectedItem.@name).@text = entryText.text;
		mlDatagrid.selectedIndex = n;
		projectModified = true;
	}
}

private function renameEntry():void {
	if (mlDatagrid.selectedIndex >= 0) {
		for each (var l:XML in project.locale) {
			l.entry.(@name == lastEntryName)[0].@name = newEntryName.text;
		} 
		//project.locale.(@code == masterLocale).entry.(@name == mlDatagrid.selectedItem.@name).@name = newEntryName.text;
		//oldEntryName.text = newEntryName.text;
		lastEntryName = newEntryName.text;
		projectModified = true;
	}
}

private function clearAllInMlTab():void {
	entryText.text = "";
	oldEntryName.text = "";
	newEntryName.text = "";
}